package com.yan_jiu_sheng.LeetCodeHot100.AC;

/**
 * https://leetcode.cn/problems/search-in-rotated-sorted-array/?envType=study-plan-v2&envId=top-100-liked
 * 自己实现的思路是 先找到旋转点 然后再进行二分查找，索引偏移    但复杂度为2log n
 * 参考题解：https://leetcode.cn/problems/search-in-rotated-sorted-array/solutions/220083/sou-suo-xuan-zhuan-pai-xu-shu-zu-by-leetcode-solut
 * 通过
 *
 * @author yulongTian
 * @create 2024-07-10 14:28
 */
public class Test64 {
    public static void main(String[] args) {
        int[] nums = {4, 5, 6, 7, 0, 1, 2};
        int target = 6;
        System.out.println(new Test64().search(nums, target));
    }

    public int search(int[] nums, int target) {
        int n = nums.length;
        if (n == 0) {
            return -1;
        }
        if (n == 1) {
            return nums[0] == target ? 0 : -1;
        }
        int l = 0, r = n - 1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[0] <= nums[mid]) {
                if (nums[0] <= target && target < nums[mid]) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            } else {
                if (nums[mid] < target && target <= nums[n - 1]) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
        }
        return -1;
    }

//    public int search(int[] nums, int target) {
//        int ans = -1;
//        int left = 0;
//        int right = nums.length - 1;
//        int mid = -1;
//        int len = nums.length;
//        if (nums[0] < nums[len - 1]) {
//            // 无旋转  直接二分
//            while (left <= right) {
//                mid = left + (right - left) / 2;
//                if (target < nums[mid]) {
//                    right = mid - 1;
//                } else if (target > nums[mid]) {
//                    left = mid + 1;
//                } else {
//                    ans = mid;
//                    break;
//                }
//            }
//        } else {
//            // 先找到转折起始点
//            while (left <= right) {
//                mid = left + (right - left) / 2;
//                if (nums[mid] > nums[len - 1]) {
//                    left = mid + 1;
//                } else if (nums[mid] < nums[0]) {
//                    right = mid - 1;
//                }
//            }
//
//            int start = left;
//            right = start + len - 1;
//            while (left <= right) {
//                int temp_mid = left + (right - left) / 2;
//                mid = temp_mid % len;
//                if (target < nums[mid]) {
//                    right = temp_mid - 1;
//                } else if (target > nums[mid]) {
//                    left = temp_mid + 1;
//                } else {
//                    ans = mid;
//                    break;
//                }
//            }
//        }
//
//        return ans;
//    }

}
